/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import <Cashbox/CBLabel.h>
#import <Cashbox/CBObject.h>
#import <Cashbox/CBDataControllerProtocol.h>

@interface CBLabel (Private)
+ (NSString *)transactionsKey;
+ (NSString *)savedColorKey;
@end

@interface CBLabel (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber *)primitiveIndex;
- (void)setPrimitiveIndex:(NSNumber *)value;
@end

@implementation CBLabel

+ (CBLabel *)labelWithName:(NSString *)name {
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
	[request setEntity:[self entity]];
	[request setPredicate:[NSPredicate predicateWithFormat:@"name = %@", name]];
	[request setSortDescriptors:
	 [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] autorelease]]];
	[request setFetchLimit:1];
	return [[[[[CBObject sharedCashboxInstance] dataController] managedObjectContext]
			 executeFetchRequest:request error:NULL] firstObject];
}

- (id)initWithIndex:(int)anIndex andInsertIntoManagedObjectContext:(NSManagedObjectContext *)context {
	if ((self = [super initWithEntity:[[self class] entity] insertIntoManagedObjectContext:context])) {
		[self setIndex:anIndex];
	}
	return self;
}

#pragma mark getters/setters
// ----------------------------------------------------------------------------------------------------
// getters/setters
// ----------------------------------------------------------------------------------------------------

@dynamic name;
@dynamic color;

- (int)index {
    NSNumber * tmpValue;
    [self willAccessValueForKey:@"primitiveIndex"];
    tmpValue = [self primitiveIndex];
    [self didAccessValueForKey:@"primitiveIndex"];
    return (tmpValue == nil) ? 0 : [tmpValue intValue];
}

- (void)setIndex:(int)value {
	NSNumber *temp = [[NSNumber alloc] initWithInt:value];
    [self willChangeValueForKey:@"primitiveIndex"];
    [self setPrimitiveIndex:temp];
    [self didChangeValueForKey:@"primitiveIndex"];
	[temp release];
}

@end
